<!--
  ~ Copyright 2020. JiaXiaohei easyDebug.net
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~  Unless required by applicable law or agreed to in writing, software
  ~  distributed under the License is distributed on an "AS IS" BASIS,
  ~  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~  See the License for the specific language governing permissions and
  ~  limitations under the License.
  -->

<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>DelayQ Console</title>
    <link rel="stylesheet" th:href="@{/plugin/layui/css/layui.css}" media="all">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <div class="layui-header">
        <div class="layui-logo" style="font-size:20px">DelayQ</div>
    </div>

    <div class="layui-side layui-bg-black">
        <div class="layui-side-scroll">
            <ul class="layui-nav layui-nav-tree">
                <li class="layui-nav-item layui-this"><a href="index">任务列表</a></li>
                <li class="layui-nav-item"><a href="status">系统状态</a></li>
            </ul>
        </div>
    </div>

    <div class="layui-body">
        <!--    <div class="layui-body" style="left:0;right:0">-->
        <!-- 内容主体区域 -->
        <div style="padding: 15px;">

            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
                <legend>任务列表</legend>
            </fieldset>

            <div class="demoTable">
                ID：
                <div class="layui-inline">
                    <input class="layui-input" name="input_id" id="input_id" autocomplete="off">
                </div>

                Job类型：
                <div class="layui-inline">
                    <input class="layui-input" name="input_topic" id="input_topic" autocomplete="off">
                </div>

                状态：
                <div class="layui-inline">
                    <input class="layui-input" name="input_status" id="input_status" autocomplete="off">
                </div>

                <button class="layui-btn" data-type="reload">搜索</button>
            </div>

            <table class="layui-hide" id="lay_table" lay-filter="table-lay-filter"></table>

            <script type="text/html" id="barDemo">
<!--                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>-->
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>


        </div>
    </div>

    <div class="layui-footer">
        <!-- 底部固定区域 -->
        © 2020 DelayQ The Apache License
    </div>
</div>

<script th:src="@{/plugin/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/js/window.js}" charset="utf-8"></script>

<script>
layui.use('table', function(){
  var table = layui.table;

  table.render({
    autoSort: false
    ,elem: '#lay_table'
    ,url:'list'
    ,response: {
        statusCode: 1 //规定成功的状态码，默认：0
        ,countName: 'total' //规定数据总数的字段名称，默认：count
    }
    ,request: {
        pageName: 'pageNum' //页码的参数名称，默认：page
        ,limitName: 'pageSize' //每页数据量的参数名，默认：limit

    }
    ,title: '任务列表'
    ,cols: [
    [
      {type: 'checkbox', fixed: 'left'}
      ,{field:'id', title:'ID', width:200, fixed: 'left',  sort: true}
      ,{field:'topic', title:'Job类型', width:180, fixed: 'left'}
      ,{field:'delay', title:'延迟时间(s)', width:150, fixed: 'left'}
      ,{field:'ttr', title:'超时时间(s)', width:150, fixed: 'left'}
      ,{field:'status', title:'状态', width:150, fixed: 'left'}
      ,{field:'executionTime', title:'执行(预计)时间', width:180, fixed: 'left',  sort: true}
      ,{field:'createTime', title:'创建时间', width:180, fixed: 'left',  sort: true}
      ,{fixed:'right', title:'操作', toolbar: '#barDemo', width:150}
    ]
    ]
    ,page: true
    ,limit: 20
    ,id: 'tableRenderId'
  });

  //监听行工具事件
  table.on('tool(table-lay-filter)', function(obj){

    var objdata = obj.data;
    if(obj.event === 'del'){
      layer.confirm('真的删除这条任务么？', function(index){
        $.ajax({
          type: 'POST',
          url: 'del',
          dataType : "json",
          data: {
            'id': objdata.id
          },
          success : function(data) {
            if(1==data.code){
             openSuccess('执行成功');
             active.reload();
            }else{
             openWarn(data.msg);
            }
          },
          error:function(XMLHttpRequest, textStatus){
             openError(XMLHttpRequest + ' ' + textStatus);
          }
        });
        layer.close(index);
      });
    } else if(obj.event === 'edit'){

<!--      layer.prompt({-->
<!--        formType: 2-->
<!--        ,value: data-->
<!--      }, function(value, index){-->
<!--        obj.update({-->
<!--          email: value-->
<!--        });-->
<!--        layer.close(index);-->
<!--      });-->
    openWindow('编辑任务：'+ objdata.id,'page/edit?id=' + objdata.id);
    }

  });

  table.on('sort(table-lay-filter)', function(obj){
    table.reload('tableRenderId', {
      initSort: obj
      ,where: {
        field: obj.field
        ,order: obj.type
      }
    });
  });

  var $ = layui.$, active = {
    reload: function(){
      var input_id = $('#input_id');
      var input_topic = $('#input_topic');
      var input_status = $('#input_status');

      //执行重载
      table.reload('tableRenderId', {
        page: {
          curr: 1
        }
        ,where: {
            id: input_id.val()
            ,topic: input_topic.val()
            ,status: input_status.val()
        }
      }, 'data');
    }
  };

   $('.demoTable .layui-btn').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });

});


</script>

</body>
</html>